package com.qfedu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.MessageEntity;
import com.qfedu.entity.RouteEntity;
import com.qfedu.entity.RouteGroupEntity;
import com.qfedu.entity.UserEntity;
import com.qfedu.kit.FruitsFinal;
import com.qfedu.kit.StringUtil;
import com.qfedu.service.RouteService;
import com.qfedu.service.TicklingService;
import com.qfedu.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class UserController {

    @Reference(check = false)
    UserService userService;
    @Reference(check = false)
    RouteService routeService;

    @Reference(check = false)
    TicklingService ticklingService;

    //跳转登录界面
    @RequestMapping("jumplogin")
    public String jumplogin(){

        return "login";
    }

    //登出
    @RequestMapping("dt")
    public String dongchu(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return "redirect:jump";
    }

    //跳转首页
    @RequestMapping("/")
    public String jumpind(HttpServletRequest request){

        //判断用户是否登录
        UserEntity user = (UserEntity) request.getSession().getAttribute("user");
        if (user == null){

            request.getSession().setAttribute("yy",0);//没有登录
        }else {
            request.getSession().setAttribute("yy",3);//已经登录

            String unc = user.getUnc();
            if (StringUtil.isEnpty(unc)){
                request.getSession().setAttribute("mm",0);//判断昵称不存在
            }else {
                request.getSession().setAttribute("mm",3);//有昵称

            }
        }
        //首页展示的一些
        RouteEntity re = new RouteEntity();
        PageInfo<RouteEntity> list = routeService.queryList(re);
        List<RouteGroupEntity> routeGroupEntities = routeService.queryGroupList();
        request.getSession().setAttribute("routegroup",routeGroupEntities);
        request.getSession().setAttribute("routrs",list);


        //首页滚动条
        List<MessageEntity> querytickall = ticklingService.querytickall();
        request.getSession().setAttribute("mtll",querytickall);
        return "index";
    }

    //跳转首页
    @RequestMapping("jump")
    public String jumpindex(HttpServletRequest request){

        //判断用户是否登录
        UserEntity user = (UserEntity) request.getSession().getAttribute("user");
        if (user == null){

            request.getSession().setAttribute("yy",0);//没有登录
        }else {
            request.getSession().setAttribute("yy",3);//已经登录

            String unc = user.getUnc();
            if (StringUtil.isEnpty(unc)){
                request.getSession().setAttribute("mm",0);//判断昵称不存在
            }else {
                request.getSession().setAttribute("mm",3);//有昵称

            }
        }
        //首页展示的一些
        RouteEntity re = new RouteEntity();
        PageInfo<RouteEntity> list = routeService.queryList(re);
        List<RouteGroupEntity> routeGroupEntities = routeService.queryGroupList();
        request.getSession().setAttribute("routegroup",routeGroupEntities);
        request.getSession().setAttribute("routrs",list);

        //首页滚动条
        List<MessageEntity> querytickall = ticklingService.querytickall();
        request.getSession().setAttribute("mtll",querytickall);


        return "index";
    }
    //跳转注册界面
    @RequestMapping("jumpregister")
    public String jumpregister(HttpServletRequest request){

//        request.setAttribute("xx",null);
        return "register";
    }


    //登录
    @RequestMapping("login")
    @ResponseBody
    public String login(UserEntity ue,HttpServletRequest request){

        //判断用户名密码是否为空
        if (StringUtil.isEnpty(ue.getUname()) || StringUtil.isEnpty(ue.getUpass())) {
            return FruitsFinal.NAME_NULL_RESULT;//返回1001

        }

        ue.setUpass(StringUtil.MD5Encode(ue.getUpass()));
        //登录验证
        UserEntity login = userService.login(ue);
        if (login == null){
            return FruitsFinal.FILED_RESULT;//返回0001没查到该用户
        }
        request.getSession().setAttribute("user",login);
        return FruitsFinal.SUCCESS_RESULT;//登录成功返回0000
    }

    //用户名唯一校验
    @RequestMapping("uniquerName")
    @ResponseBody
    public String uniqueName(String uaccount){

        UserEntity ue = new UserEntity();
        ue.setUname(uaccount);
        UserEntity user = userService.queryuser(ue);
        if (user!=null && !user.equals("")){
            return "不可用";
        }else{
            return "0000";
        }

    }


    //注册
    @RequestMapping("register")
    @ResponseBody
    public String registera(UserEntity ue, HttpServletRequest request){
        //检测用户名密码是否为空
        String uname = ue.getUname();
        String upass = ue.getUpass();
        if (StringUtil.isEnpty(uname) || StringUtil.isEnpty(upass)){

            //request.setAttribute("xx","用户名密码不能为空！");
            return FruitsFinal.FILED_RESULT;//0001用户名或密码为空
        }
        //账号重复返回注册界面
        UserEntity ee = new UserEntity();
        ee.setUname(ue.getUname());
        UserEntity queryuser = userService.queryuser(ee);
        if (queryuser!=null){
            return FruitsFinal.NAME_NULL_RESULT;//用户已存在1001
        }
        //开始注册
        ue.setUpass(StringUtil.MD5Encode(ue.getUpass()));
        boolean regist = userService.regist(ue);
        if (!regist){
            //request.setAttribute("xx","系统异常！");
            return FruitsFinal.ROLE_ADD_FILED_RESULT;//2001系统异常
        }

        return FruitsFinal.SUCCESS_RESULT;//成功0000
    }

    //跳转会员中心
    @RequestMapping("jumpmembercenter")
    public String jumpmembercenter(HttpServletRequest request){
        UserEntity user = (UserEntity) request.getSession().getAttribute("user");
        if (user==null){
            return "login";
        }
        //判断一些关键属性是否为空
        if (user.getUphone()==null){
            user.setUphone("");
        }
        if (user.getUnc()==null){
            user.setUnc("");
        }
        if (user.getUemail()==null){
            user.setUemail("");
        }
        if (user.getUqq()==null){
            user.setUqq("");
        }
        if (user.getUwechat()==null){
            user.setUwechat("");
        }

        return "membercenter";
    }

    //部分信息的修改
    @RequestMapping("upduser")
    public String upduser(UserEntity ue,HttpServletRequest request){

        boolean upduser = userService.upduser(ue);
        //修改成功将用户新的信息存入session
        if (upduser){
            //request.getSession().removeAttribute("user");
            UserEntity queryuser = userService.queryuser(ue);
            request.getSession().setAttribute("user",queryuser);
            System.out.println(queryuser);
        }

        return "redirect:jumpmembercenter";
    }

    @RequestMapping("jumpchange")
    public String jumpchange(){

        return "change";
    }

    @RequestMapping("changepass")
    @ResponseBody
    public String changepass(UserEntity ue,HttpServletRequest request){
//        String uid = request.getParameter("uid");
//        String upass = request.getParameter("upass");
        String newpass = request.getParameter("newpass");
        String newpas = request.getParameter("newpas");

//        if (true){
//            return "";
//        }
        if (!newpas.equals(newpass)){
            return FruitsFinal.FILED_RESULT;//两次新密码不一致返回0001
        }
        if (StringUtil.isEnpty(newpas)){
            return FruitsFinal.ROLE_ADD_FILED_RESULT;//新密码是空返回2001
        }
        ue.setUpass(StringUtil.MD5Encode(ue.getUpass()));
        UserEntity login = userService.login(ue);//查询密码是否正确
        if (login==null){
            return FruitsFinal.NAME_NULL_RESULT;//用户密码错误返回1001
        }
        ue.setUpass(StringUtil.MD5Encode(newpas));
        boolean upduser = userService.upduser(ue);
        if (upduser){

            return FruitsFinal.SUCCESS_RESULT;//密码修改成功
        }

        return "";
    }


    //留言明细
    @RequestMapping("mingxi")
    @ResponseBody
    public String mingxi(int lid,HttpServletRequest request){
        MessageEntity querytuck = ticklingService.querytuck(lid);
        if (querytuck!=null){

            if (querytuck.getLcontent()==null){
                querytuck.setLcontent("");
            }
            if (querytuck.getLemail()==null){
                querytuck.setLemail("");
            }
            if (querytuck.getLname()==null){
                querytuck.setLname("");
            }
            if (querytuck.getLphone()==null){
                querytuck.setLphone("");
            }
            if (querytuck.getLradio()==null){
                querytuck.setLradio("");
            }
            if (querytuck.getLtitle()==null){
                querytuck.setLtitle("");
            }

            request.getSession().setAttribute("liuyan",querytuck);

            return FruitsFinal.SUCCESS_RESULT;//0000

        }

        return "";
    }

    //跳转留言详情页面
    @RequestMapping("ceshi")
    public String ceshi(){

        return "onetick";
    }




}
